import Pages from '../../../pages/pages.vue'
import Confirm from '../../../confirm/confirm.vue'
import Tab from '../tab/tab.vue'
import { getUserListExcel, sentPrice, showMsg, setMenuType } from '../../../../vuex/action'

export default {
  name: 'user-list',
  components: {
    'vue-pages': Pages,
    'vue-confirm': Confirm,
    'vue-tab': Tab
  },
  data() {
    return {
      phone: '',
      status: '',
      checkedNames: [],
      checkAll: false,
      noPrice: true,
      userList: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      }
    }
  },
  vuex: {
    getters: {
      fresh: state => state.dataflowListNeedFresh
    },
    actions: {
      xGetUserListExcel: getUserListExcel,
      xSentPrice: sentPrice,
      xShowMsg: showMsg,
      xSetMenuType: setMenuType
    }
  },
  ready() {
    this.xSetMenuType('portfolio')
    this.getList({})
  },
  methods: {
    getList({phone=this.phone, send=this.status}) {
      this.xGetUserListExcel({
        phone: phone,
        send: send
      })
        .then((data) => {
          this.checkAll = false
          this.userList = data.list
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    sent() {
      this.xSentPrice({
        send: 1,
        phones: this.checkedNames.join()
      })
        .then((data) => {
          this.xShowMsg({
            type: 'info',
            content: '操作成功'
          })
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    noSent() {
      this.xSentPrice({
        send: 2,
        phones: this.checkedNames.join()
      })
        .then((data) => {
          this.xShowMsg({
            type: 'info',
            content: '操作成功'
          })
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getList({})
      }
    },
    checkAll() {
      this.checkedNames = []
      for (var item of this.userList) {
        if (this.checkAll) {
          this.checkedNames.push(item.phone + '')
        }
      }
    },
    checkedNames() {
      if (this.checkedNames.length === 0) {
        this.noPrice = true
      } else {
        this.noPrice = false
      }
    }
  }
}
